package libs.yzx.zaza.acts2;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

public class GlideActivity extends AppCompatActivity {

    public static class LifeFragment extends Fragment{
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Toast.makeText(getActivity(), "created", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            Toast.makeText(getActivity(), "activity created", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onDestroyView() {
            super.onDestroyView();
            Toast.makeText(getActivity(), "destory view", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onStop() {
            super.onStop();
            Toast.makeText(getActivity(), "stop", Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportFragmentManager().beginTransaction().add(new LifeFragment(),"lifeCycle_F").commit();
    }


    /**
     * 单例示例
     */
    private static class Singleton{
        private static volatile Singleton single;

        public static Singleton getInstance(){
            if(single == null){
                aa:
                synchronized (Singleton.class){
                    /* 这块加判断,是因为可能多个线程同时阻塞在aa处,当一个线程执行完了,同步块里边的东西还会被其他线程执行,所以要加判断,同事单例要用volatile修饰,保持多线程的可见性 */
                    if(single == null)
                        single = new Singleton();
                }
            }
            return single;
        }

    }


}
